<?php 
define("__ACTIVE_PAGE__", "profile");
require_once dirname(__FILE__).'/init.php'; 
require_once dirname(__FILE__).'/SimpleImage.php';
//avatarUrl
if ($_POST){
	$profile = new CompanyProfileInfo($company->profileInfoId);
	$profile->name = $_POST["name"];
	$profile->email = $_POST["email"];
	$profile->phone = $_POST["phone"];
	$profile->address = $_POST["address"];
  $profile->tags = $_POST["category"];
	$profile->save(false);



	$user = User::getByUserName($company->username);
	$user->longitud = $_POST["longitud"];
	$user->latitud = $_POST["latitud"];


  if (!empty($_POST["logo-url"])){
    $imgHandler = new SimpleImage();
    $imgHandler->load($_POST["logo-url"]);
    $imgHandler->resizeToWidth(150);
    $tmp_file = "/tmp/".md5(time().rand(0, 999));
    $imgHandler->save($tmp_file);

    $user->updateUserIcon(file_get_contents($tmp_file)); 
  }
  
	
	$user->save(false);
	loginUser($user, true);
	
	
	header("location:index.php");
}

?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php require dirname(__FILE__).'/common/head.php'; ?>
	</head>
	<body>
		<style type="text/css">

div#map {
	position: relative;
  width:100%;
  height:400;
  border: 1px solid #333;
}

div#map_canvas {
  width:100%;
  height:400px;
}

div#map .zoom{
  display: none;
}

div#map .form{
  position: absolute;
  top: -54px;
  left: 50%;
  width:980px;
  height:50px;
  margin:0 0 0 -490px;
  text-align: center;
  line-height: 50px;
  color: #fff;
  background: #FFFD8B;
  -webkit-border-bottom-right-radius: 2px;
  -webkit-border-bottom-left-radius: 2px;
  -moz-border-radius-bottomright: 2px;
  -moz-border-radius-bottomleft: 2px;
  border-bottom-right-radius: 2px;
  border-bottom-left-radius: 2px;
  z-index: 1;
}

div#map .form .google{
  position: absolute;
  top: 7px;
  left: 14px;
  height: 30px;
  z-index: 1;
}
div#map .coordinates{
  position: absolute;
  bottom: 20px;
  left: 50%;
  width:499px;
  height:40px;
  margin:0 0 0 -250px;
  text-align: center;
  line-height: 50px;
  color: #fff;
  z-index: 1;
}
div#map .coordinates em{
  position: absolute;
  top: -20px;
  width: 249px;
  height: 20px;
  background: #6BAB96;
  color: #000;
  font-style: normal;
  letter-spacing: 1px;
  font-size: 10px;
  line-height: 20px;
  text-transform: uppercase;
  font-weight: normal;
}
div#map .coordinates em.lat{
  left: 0;
}
div#map .coordinates em.lon{
  right: 0;
}
div#map .coordinates span{
  display: block;
  float: left;
  width: 249px;
  font-size: 18px;
  line-height: 40px;
  background: #333;
}
div#map .coordinates span#lng{
  float: right;
}
div#map .coordinates span:hover{
  background: #111;
}

div#map .address{
  position: absolute;
  bottom: -44px;
  left: 0;
  width:100%;
  height:40px;
  text-align: center;
  line-height: 40px;
  font-weight: bold;
}

div#crosshair {
	display: block;
	position: absolute;
	top: 50%;
  left: 50%;
	height: 17px;
	width: 16px;
	margin-left: -8px;
  margin-top: -8px;
	background-image: url(static/img/bg-sprite.png);
	background-position: 0 -23px;
	background-repeat: no-repeat;
}

.logohtml5 {
  display: none;
}
</style>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var map;
  var geocoder;
  var centerChangedLast;
  var reverseGeocodedLast;
  var currentReverseGeocodeResponse;

  function initialize() {
    var latlng = new google.maps.LatLng(<?php echo $company->latitud ?>,<?php echo $company->longitud ?>);
    var myOptions = {
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    geocoder = new google.maps.Geocoder();
    setupEvents();
    centerChanged();
  }

  function setupEvents() {
    reverseGeocodedLast = new Date();
    centerChangedLast = new Date();

    setInterval(function() {
      if((new Date()).getSeconds() - centerChangedLast.getSeconds() > 1) {
        if(reverseGeocodedLast.getTime() < centerChangedLast.getTime())
          reverseGeocode();
      }
    }, 1000);

    google.maps.event.addListener(map, 'zoom_changed', function() {
      document.getElementById("zoom_level").innerHTML = map.getZoom();
    });

    google.maps.event.addListener(map, 'center_changed', centerChanged);

    google.maps.event.addDomListener(document.getElementById('crosshair'),'dblclick', function() {
       map.setZoom(map.getZoom() + 1);
    });

  }

  function getCenterLatLngText() {
    return '(' + map.getCenter().lat() +', '+ map.getCenter().lng() +')';
  }

  function centerChanged() {
    centerChangedLast = new Date();
    var latlng = getCenterLatLngText();
    var lat = map.getCenter().lat();
    var lng = map.getCenter().lng();

    document.getElementById('lat').value = lat;
    document.getElementById('lng').value = lng;

    document.getElementById('formatedAddress').innerHTML = '';
    currentReverseGeocodeResponse = null;
  }

  function reverseGeocode() {
    reverseGeocodedLast = new Date();
    geocoder.geocode({latLng:map.getCenter()},reverseGeocodeResult);
  }

  function reverseGeocodeResult(results, status) {
    currentReverseGeocodeResponse = results;
    if(status == 'OK') {
      if(results.length == 0) {
        document.getElementById('formatedAddress').innerHTML = 'None';
      } else {
        document.getElementById('formatedAddress').innerHTML = results[0].formatted_address;
      }
    } else {
      document.getElementById('formatedAddress').innerHTML = 'Error';
    }
  }

  function geocode() {
    var address = document.getElementById("address").value;
    geocoder.geocode({
      'address': address,
      'partialmatch': true}, geocodeResult);
  }

  function geocodeResult(results, status) {
    if (status == 'OK' && results.length > 0) {
      map.fitBounds(results[0].geometry.viewport);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  }

  function addMarkerAtCenter() {
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        map: map
    });

    var text = 'Lat/Lng: ' + getCenterLatLngText();
    if(currentReverseGeocodeResponse) {
      var addr = '';
      if(currentReverseGeocodeResponse.size == 0) {
        addr = 'None';
      } else {
        addr = currentReverseGeocodeResponse[0].formatted_address;
      }
      text = text + '<br>' + 'Dirección: <br>' + addr;
    }

    var infowindow = new google.maps.InfoWindow({ content: text });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  }
</script>
		<?php require dirname(__FILE__).'/common/header.php'; ?>
		<div class="container">
			<div class="row">
				<div class="span12">
					<h4>Edit Profile</h4>
					<div class="row">
						<div class="span6">
							<form method="post" action="" class="form-horizontal">
								<div class="control-group">
									<label class="control-label" for="inputEmail">Name</label>
									<div class="controls">
										<input type="text" id="inputEmail" name="name" value="<?php echo $company->profile->name; ?>"/>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="inputEmail">Email</label>
									<div class="controls">
										<input type="text" id="inputEmail" name="email" value="<?php echo $company->profile->email; ?>"/>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="inputEmail">Telephone</label>
									<div class="controls">
										<input type="text" id="inputEmail" name="phone" value="<?php echo $company->profile->phone; ?>"/>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="inputEmail">Address</label>
									<div class="controls">
										<input type="text" id="inputEmail" name="address" value="<?php echo $company->profile->address; ?>"/>
									</div>
								</div>
                <div class="control-group">
                  <label class="control-label" for="inputEmail">Category</label>
                  <div class="controls">
                    <select name="category">
                      <?php foreach (Profile::getInterestsList() as $id => $category): ?>
                        <option value="<?php echo $id; ?>" <?php echo ($id == $company->profile->tags) ? "selected" : ""; ?>><?php echo $category; ?></option>
                      <?php endforeach; ?>
                    </select>
                  </div>
                </div>
								<div class="control-group">
									<label class="control-label" for="inputEmail">Latitud</label>
									<div class="controls">
										<input readonly type="text" id="lat" name="latitud" value="<?php echo $company->latitud; ?>"/>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="inputEmail">Longitud</label>
									<div class="controls">
										<input readonly type="text" id="lng" name="longitud" value="<?php echo $company->longitud; ?>"/>
									</div>
								</div>
								<div class="control-group">
									<label class="control-label" for="inputEmail">Logo from Url:</label>
									<div class="controls">
										<input type="text" id="inputUrl" name="logo-url" value=""/>
									</div>
								</div>
								<div class="control-group">
									<div class="controls">
										<button type="submit" class="btn btn-primary">Save</button>
										<a href="index.php" class="btn">Cancel</a>
									</div>
								</div>
							</form>
						</div>
						<div class="span6">
							<h5>Location</h5>
							<div id="map">
							    <div id="map_canvas"></div>
							    <div id="crosshair"></div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<?php require dirname(__FILE__).'/common/footer.php'; ?>
		</div> 
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
		<script>
		$(document).ready(function(){
		  initialize();
		});
		</script>
	</body>
</html>







